/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tamnhat.entity;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class Publisher implements Serializable{
    private String id;
    private String code;
    private String name;

    public Publisher() {
    }

    public Publisher(String name) {
        this.name = name;
    }

    public Publisher(String id, String code, String name) {
        this.id = id;
        this.code = code;
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return name;
    }    

    public static List<Publisher> allPub(String condition) throws Exception{
        List<Publisher> list = new ArrayList<Publisher>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = " select * from md_publishers "
                + " "+condition
                + " order by publisher_id";
        
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                Publisher p  = new Publisher(); 
                p.setId(rs.getString(1));
                p.setCode(rs.getString(2));
                p.setName(rs.getString(3));
                list.add(p);
            }            
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return list;
    }
    public String getIdByName() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String kq = "";
        
        if(getName() == null){
            return kq;
        }
        String strSQL = " select * from md_publishers where upper(name) = ? ";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareCall(strSQL);
            
            pst.setString(1, getName().toUpperCase());
            rs = pst.executeQuery();
            if(rs.next()){
                kq = rs.getString(1);
            }            
            return kq;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    
    public static String getIdByName(String name) throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String kq = "";
        
        if(name == null){
            return kq;
        }
        String strSQL = " select * from md_publishers where upper(name) = ? ";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareCall(strSQL);
            
            pst.setString(1, name.toUpperCase());
            rs = pst.executeQuery();
            if(rs.next()){
                kq = rs.getString(1);
            }            
            return kq;
        } finally {
            Database.closeObject(rs);
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    
    public boolean insert() throws Exception{
        Connection cn = null;
        PreparedStatement pst = null;        
        try {
            cn = AppServer.getConnection();
            setId(Database.getSequenceValue(cn, "seq_common"));
            String strSQL = " insert into md_publishers values(?,?,?) ";
            pst = cn.prepareStatement(strSQL);
            
            pst.setInt(1, Integer.parseInt(getId()));
            pst.setString(2, getCode());
            pst.setString(3, getName());
            return pst.executeUpdate() > 0;            
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    
    public boolean remove() throws Exception{
        Connection cn = null;
        PreparedStatement stmt = null;
        String strSQL = " delete from md_publishers where publisher_id = "+getId()+" ";
        try {
            cn = AppServer.getConnection();
            stmt = cn.prepareStatement(strSQL);
            return stmt.executeUpdate() > 0;            
        } finally {
            Database.closeObject(stmt);
            Database.closeObject(cn);
        }
    }
    
    public boolean update() throws Exception{
        Connection cn = null;
        PreparedStatement stmt = null;
        String strSQL = " update md_publishers"
                + " set code = ?, name = ?"
                + " where publisher_id = ? ";
        try {
            cn = AppServer.getConnection();
            stmt = cn.prepareStatement(strSQL);
            
            stmt.setString(1, getCode());
            stmt.setString(2, getName());
            stmt.setInt(3, Integer.parseInt(getId()));
            
            return  stmt.executeUpdate() > 0;
        } finally {
            Database.closeObject(stmt);
            Database.closeObject(cn);
        }
    }
}
